#include <brpc/channel.h>
#include <thread>
#include "main.pb.h"

void callback(brpc::Controller* cntl, ::example::EchoResponse *response)
{
    std::unique_ptr<brpc::Controller> cntl_guard(cntl);
    std::unique_ptr<::example::EchoResponse> resp_guard(response);
    if (cntl_guard->Failed() == true) 
    {
        std::cout << "Rpc调用失败: " << cntl_guard->ErrorText() << std::endl;
        return;
    }
    std::cout << "收到响应: " << resp_guard->message() << std::endl;
}

int main()
{
    // 1.构建channel信道，连接服务器
    brpc::Channel channel;
    brpc::ChannelOptions options;
    options.connect_timeout_ms = -1; // 连接等待超时时间，-1表示一直等待
    options.timeout_ms = -1;         // rpc请求等待超时时间，-1表示一直等待
    options.max_retry = 3;           // 请求重试次数上限
    options.protocol = "baidu_std";  // 序列化协议，默认使用baidu_std
    if (channel.Init("127.0.0.1:8888", &options) == -1)
    {
        std::cout << "初始化信道失败！" << std::endl;
        return -1;
    }
    // 2.构造EchoService_Stub对象，用于进行rpc调用
    example::EchoService_Stub stub(&channel);
    // 3.进行Rpc调用
    example::EchoRequest req;
    req.set_message("Hello World! ");
    brpc::Controller* cntl = new brpc::Controller();
    example::EchoResponse* resp = new example::EchoResponse();
    auto clusure = google::protobuf::NewCallback(callback,cntl,resp);
    stub.Echo(cntl, &req, resp, clusure); //异步调用
    std::this_thread::sleep_for(std::chrono::seconds(3));
    std::cout << "异步调用结束" << std::endl; 
    // 同步调用
    //stub.Echo(cntl, &req, resp, nullptr);
    // if (cntl->Failed() == true) 
    // {
    //     std::cout << "Rpc调用失败: " << cntl->ErrorText() << std::endl;
    //     return -1;
    // }
    // std::cout << "收到响应: " << resp->message() << std::endl;
    // delete cntl;
    // delete resp;
    return 0;
}